﻿AK_channelName="";
AK_Buffer=nil;
AK_Cf={};
AK_timer=0;
AK_interval=0.3;
AK_lastSender=nil;
AK_BlackList={}
AK_bTimer=0;
AK_bInterval=1;
AK_bLife = 15;

function AK_OnLoad()	
	OldChatFrame_OnEvent = ChatFrame_OnEvent;
	ChatFrame_OnEvent = AK_ChatFrame_OnEvent;
	DEFAULT_CHAT_FRAME.Old_AddMessage = DEFAULT_CHAT_FRAME.AddMessage;
	DEFAULT_CHAT_FRAME.AddMessage = AK_AddMessage;
	AK_ID=DEFAULT_CHAT_FRAME:GetID();
end

function AK_ChatFrame_OnEvent(event)
	if event=="CHAT_MSG_CHANNEL" and this:GetID()==AK_ID then
		if AK_BFind(arg1,arg2) then
			return
		else
			AK_BAdd(arg1,arg2);
		end
		AK_capture=true;
		if AK_Buffer~=nil then
			if strfind(AK_Buffer["msg"],arg2) then
				AK_Buffer["a9"]=true;
				AK_capture=false;AK_timer=0;return;
			else
				AK_capture=false;AK_timer=0;
			end
		end
	end
	OldChatFrame_OnEvent(event);
end

function AK_OnUpdate(elapsed)
	if AK_BlackList and getn(AK_BlackList)>0 then
		AK_bTimer = AK_bTimer + elapsed;
		if AK_bTimer > AK_bInterval then
			for i =1,getn(AK_BlackList) do
				if AK_BlackList[i] then
					AK_BlackList[i]["timer"] = AK_BlackList[i]["timer"] + AK_bTimer;
					if AK_BlackList[i]["timer"]>AK_bLife then
						tremove(AK_BlackList,i);
					end
				end
			end
			AK_bTimer=0;
		end
	else
		AK_bTimer=0;
	end
	if AK_Buffer~=nil then
		AK_timer = AK_timer + elapsed;
		if AK_timer>AK_interval then
			AK_Send();
		end
	else
		AK_timer=0;
	end
end

function AK_BFind(a1,a2)
	for i =1,getn(AK_BlackList) do
		if AK_BlackList[i] then
			if AK_BlackList[i]["arg1"] ==a1 and AK_BlackList[i]["arg2"] ==a2 then
				AK_BlackList[i]["timer"] = 0;return true;
			end
		end
	end
	return false;
end

function AK_BAdd(a1,a2)
	for i =1,getn(AK_BlackList) do
		if AK_BlackList[i] then
			if AK_BlackList[i]["arg1"] ==a1 and AK_BlackList[i]["arg2"] ==a2 then
				 return;
			end
		end
	end
	tinsert(AK_BlackList,{["timer"]=0,["arg1"]=a1,["arg2"]=a2});
end

function AK_AddMessage(self,msg, a1,a2,a3,a4,a5,a6,a7,a8,a9)
	if self:GetID()==AK_ID and AK_capture then
			AK_Buffer={};AK_Buffer["a9"]=false;AK_Buffer["msg"],AK_Buffer["a1"],AK_Buffer["a2"],AK_Buffer["a3"],AK_Buffer["a4"],AK_Buffer["a5"],AK_Buffer["a6"],AK_Buffer["a7"],AK_Buffer["a8"]=msg,a1,a2,a3,a4,a5,a6,a7,a8;
			AK_capture=false;return;
	end
	if a9==true then return end
	return self:Old_AddMessage(msg, a1,a2,a3,a4,a5,a6,a7,a8,a9);
end

function AK_Send()
	DEFAULT_CHAT_FRAME:AddMessage(AK_Buffer["msg"],AK_Buffer["a1"],AK_Buffer["a2"],AK_Buffer["a3"],AK_Buffer["a4"],AK_Buffer["a5"],AK_Buffer["a6"],AK_Buffer["a7"],AK_Buffer["a8"],AK_Buffer["a9"])
	AK_Buffer=nil;AK_last=nil;
end

function print(msg)
	DEFAULT_CHAT_FRAME:AddMessage(msg)
end